<?php
namespace app\backend\controller;

use app\backend\model\Address as AddressModel;
use app\backend\model\contact as ContactModel;
use think\Db;
use think\Request;
use app\backend\org\Helper;
use app\backend\org\Image;

class Contact extends Base
{

    // 列表展示
    public function index($con_id=1)
    {
        $con_id = Request::instance()->param("con_id");
        $model = new AddressModel;
        // 获取分页显示
        $result = $model->getAll($con_id)->paginate(5);
        $page = $result->render();
        $this->assign('page',$page);
        $this->assign('address',$result);
        $this->assign('empty',"暂无数据");
        //$this->assign('jsController','people');
       $this->assign('jsId',$con_id);
        return $this->fetch();

    }

    //新建
    public function create(){
        if (Request::instance()->isPost())
        {
            $model = new AddressModel();
            $post = Request::instance()->post();// 获取经过过滤的全部post变量

            // 判断已有内容不能重复新增去修改
            $name = Db::query("select add_id,title_cn from ls_address where `title_cn`='".$post['title_cn']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return $this->redirect(url('update',array('id'=>$name[0]['add_id'])));
            }

            //var_dump( $post["sort"]);exit;
            // 验证表单并插入数据库
            $result = $this->validate($post,'Address.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            //$team_id = Request::instance()->param("team_id");
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('contact/index',["con_id"=>$post["p_id"]]);
            }
        }
        return $this->fetch();
    }

    //修改
    public function update($id)
    {
        if (Request::instance()->isPost())
        {
            $model = new AddressModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量

            //验证表单并插入数据库
            $result = $this->validate($post,'Address.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['add_id'=>$id]);

            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->success('修改成功',url('contact/index',['con_id'=>$post["p_id"]]));
            }
        }

        //var_dump($depart_old);exit;
        //根据主键插记录
        $record = AddressModel::get($id);
        //var_dump($record);exit;
        $this->assign('id',$id);
        $this->assign('address', $record);
        $this->assign('act','index');
        return $this->fetch();
    }

    //删除
    public function delete($id)
    {
        //删除
        $p_id = AddressModel::where(["add_id"=>$id,"delete_time"=>["exp","is null"]])->value("p_id");
        AddressModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect(url('index',array('con_id'=>$p_id)));
        return $this->fetch();

    }

}